package com.cloud.utils.common;


import javax.servlet.http.HttpServletRequest;

import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;

/**
 * 将http请求中的解析设备号
 * @author Owner
 *
 */
public class DeviceInfo {


    public static String getDeviceType(HttpServletRequest request) {
        String agentString = request.getHeader("User-Agent");
        return  getMsg(agentString);
    }


    private  static  String getMsg(String agentString) {
        UserAgent userAgent = UserAgent.parseUserAgentString(agentString);
        // 操作系统信息
        OperatingSystem operatingSystem = userAgent.getOperatingSystem();
        // 设备类型
        eu.bitwalker.useragentutils.DeviceType deviceType = operatingSystem.getDeviceType();

        String deviceTypeN="未知";

        switch (deviceType) {
            case COMPUTER:
                deviceTypeN= "PC";
                break;
            case TABLET: {
                if (agentString.toUpperCase().contains("ANDROID")) {
                    deviceTypeN="Android Pad";
                }else if (agentString.toUpperCase().contains("IPAD")) {
                    deviceTypeN= "iPad";
                }else if(agentString.toUpperCase().contains("IPHONE")) {
                    deviceTypeN="iPhone";
                }else {
                    deviceTypeN="Unknown";
                }
                break;
            }
            case MOBILE: {
                if (agentString.toUpperCase().contains("ANDROID")) {
                    deviceTypeN="Android";
                }else if (agentString.toUpperCase().contains("IPHONE")) {
                    deviceTypeN="IPHONE";
                } else if (agentString.toUpperCase().contains("IPAD")) {
                    deviceTypeN= "iPad";
                }else {
                    deviceTypeN="Unknown";
                }
                break;
            }
            default:
                deviceTypeN= "Unknown";
                break;
        }

        return deviceTypeN+":"+operatingSystem;
    }


    public static void main(String[] args) {
        System.out.println(getMsg("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"));
    }

}
